import exifread
import cv2
import math
from math import radians


def pic_c_lat_lng(photo):
    """
    获取EXIF类型图片，的拍摄时间，和中心店经纬度
    :param photo: 图片路径
    :return:
    """

    # Open image file for reading (binary mode)
    f = open(photo, 'rb')
    # Return Exif tags
    tags = exifread.process_file(f)
    print(tags)

    try:
        # 拍摄时间
        EXIF_Date = tags["EXIF DateTimeOriginal"].printable
        # 纬度
        LatRef = tags["GPS GPSLatitudeRef"].printable
        Lat = tags["GPS GPSLatitude"].printable[1:-1].replace(" ", "").replace("/", ",").split(",")
        Lat = float(Lat[0]) + float(Lat[1]) / 60 + float(Lat[2]) / float(Lat[3]) / 3600
        if LatRef != "N":
            Lat = Lat * (-1)
        # 经度
        LonRef = tags["GPS GPSLongitudeRef"].printable
        Lon = tags["GPS GPSLongitude"].printable[1:-1].replace(" ", "").replace("/", ",").split(",")
        Lon = float(Lon[0]) + float(Lon[1]) / 60 + float(Lon[2]) / float(Lon[3]) / 3600
        if LonRef != "E":
            Lon = Lon * (-1)
        f.close()
    except:
        return "ERROR:请确保照片包含经纬度等EXIF信息。"
    else:
        return EXIF_Date, Lat, Lon

if __name__ == '__main__':
    pic_c_lat_lng("C:\\Users\\Administrator\\Desktop\\temp\\DJI_20250319155251_0079_V.JPG")